//请求
function studentShow(){
    $.ajax({
        url:'/student/studentshow',
        type:'post',
        success:function(data){
            let total='';
            data.forEach(e=>{
                total+=` <tr>
                <td>${e._id}</td>
                <td>${e.name}</td>
                <td>${e.age}</td>
                <td>${e.phone}</td>
                <td>${(e.classId).name}</td>
                <td><button class="but-red delate-stu">删除</button><button class="but-blue change-stu">修改</button><button style="display:none" class="send-stu">提交</button></td>
            </tr>`
            })
            $(`#studenttbody`).html(total)
        }
    })
}studentShow()
//添加数据
$(`#box4-btn`).click(function(event){   
    $.ajax({
        url:`/student/studentadd`,
        type: 'POST',
        data:{
           name:$(`#add-studentname`).val(),
           age:$(`#add-studentage`).val(),
           phone:$(`#add-studentphone`).val(),
           classId:$(`#add-studentclass`).val()
        },
        success: function (data) { 
          alert(data.mes)
          studentShow()
          }
        
    })
  })
  //删除数据
$(`body`).on('click','.delate-stu',function(event){
    console.log($(this).parent().prev().prev().prev().prev().prev()[0].innerText);
    delateid =$(this).parent().prev().prev().prev().prev().prev()[0].innerText.trim()
$.ajax({
    url:'/student/studentdelate',
    type:'POST',
    data:{
        _id:delateid
    },
    success: function(data){
     alert(data.mes)
     studentShow()
    }
})
})
//修改数据
$(`body`).on('click','.change-stu',function(event){
$(this).next().css('display','block')
changeclass=$(this).parent().prev()[0];
changephone=$(this).parent().prev().prev()[0];
changeage=$(this).parent().prev().prev().prev()[0];
changename=$(this).parent().prev().prev().prev().prev()[0];
changeid=$(this).parent().prev().prev().prev().prev().prev()[0].innerText.trim();
console.log(changeid);
changeclass.innerHTML=`<input id="change-class" type="text">`
changephone.innerHTML=`<input id="change-phone" type="text">`
changeage.innerHTML=`<input id="change-age" type="text">`
changename.innerHTML=`<input id="change-name" type="text">`

})
$(`body`).on('click','.send-stu',function(event){
    console.log(666);
$.ajax({
    url:'/student/studentchange',
    type:'POST',
    data:{
        name:$(`#change-name`).val(),
        classId:$(`#change-class`).val(),
        phone:$(`#change-phone`).val(),
        age:$(`#change-age`).val(),
        _id:changeid
    },
    success: function(data){
     alert(data.mes)
     studentShow()
    }
})    
})